{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "610a1561",
   "metadata": {},
   "source": [
    "## 产品需求分析\n",
    "**产品描述：** 针对5-50岁的人群 设计一款专门针对识别小昆虫的一个小程序 带你一起走进昆虫的世界  \n",
    "\n",
    "**产品类型：** 微信小程序  \n",
    "\n",
    "**核心价值：** 利用图像识别功能，通过拍照、上传照片等方式，能够快速识别照片上的内容，还支持获取识别结果的百科信息，返回百度百科的信息描述及其图片，通过图片和文字行结合的方式，从而来提高用户的体验，也在一定程度上也增强了用户对于知识的渴望  \n",
    "\n",
    "**发展优势：**  \n",
    "- 图像识别技术日益完善，且需求量不断上升\n",
    "- 目前专门用于识别昆虫类的工具特别少，发展空间广阔\n",
    "\n",
    "**IDEO三要素：**  \n",
    "*Desirability用户可欲性：*满足对一些对昆虫爱好者的用户来识别昆虫的需求，通过图文结合的卡片学习方式来帮助用户识别昆虫，可以学习相关知识  \n",
    "*Viability商业可行性：*市场上图像识别类小程序数量较多，但是...具有一定创新型，可以吸引那些对昆虫感兴趣的用户  \n",
    "*Feasibility技术可行性：*百度ai平台可以提供动物识别的api，微信小程序上手简单快捷    \n",
    "\n",
    "\n",
    "**用户痛点：**\n",
    "1. 不清楚是害虫还是益虫\n",
    "2. 看到昆虫很熟悉，但是不清楚名字\n",
    "3. 想要了解小昆虫的名字、特征、生活习惯\n",
    "\n",
    "**用户画像：** \n",
    "![](用户画像.png)\n",
    "\n",
    "**竞品分析：**  \n",
    "晓虫小程序：  \n",
    "功能：系统自动检测昆虫目标进行识别, 也支持手工选择识别区域  \n",
    "优点：支持 3525 个昆虫或其他节肢类动物分类单元 (可能是目, 科, 属, 种或形态等)  \n",
    "缺点：识别昆虫信息较少，精确度不够，尤其是毛毛虫类，会直接显示不是昆虫  \n",
    "\n",
    "**产品图：**\n",
    "![](图1.png)\n",
    "![](图2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f51be799",
   "metadata": {},
   "source": [
    "## 获取token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "db60b389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.35afd1319df4847324d05302c62c8dd2.2592000.1652243745.282335-25904750'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    \"grant_type\": \"client_credentials\",\n",
    "    \"client_id\": \"cxb09hxA168RMCdyxNFx7zZ8\",\n",
    "    \"client_secret\": \"85Nca1YweSadYpekXRxGGmiT5Gi4Xj3T\"\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "token = response.json()[\"access_token\"]\n",
    "token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79dff6a3",
   "metadata": {},
   "source": [
    "## 封装函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9d70e20d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eaa48d36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': [{'name': '毛虫', 'score': '0.411778'},\n",
       "  {'name': '红脚毛毛虫', 'score': '0.246679'},\n",
       "  {'name': '毒蛾', 'score': '0.156326'},\n",
       "  {'name': '榕透翅毒蛾', 'score': '0.0475912'},\n",
       "  {'name': '洋辣子', 'score': '0.022948'},\n",
       "  {'name': '舞毒蛾', 'score': '0.0228775'}],\n",
       " 'log_id': 1513375244887464919}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "animal_info = everything(token,\"毛毛虫.jpg\")\n",
    "animal_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f9884f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': [{'name': '青蜂', 'score': '0.943458'},\n",
       "  {'name': '丝光绿蝇', 'score': '0.00988653'},\n",
       "  {'name': '虎甲', 'score': '0.00524588'},\n",
       "  {'name': '叶齿金绿泥蜂', 'score': '0.00504721'},\n",
       "  {'name': '吉丁虫', 'score': '0.00387995'},\n",
       "  {'name': '大紫艳金花虫', 'score': '0.00300638'}],\n",
       " 'log_id': 1513375248845429615}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "animal_info1 = everything(token,\"昆虫.jpg\")\n",
    "animal_info1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "26c8bdd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.baidu.com/s?wd=%E9%9D%92%E8%9C%82\n",
      "227\n"
     ]
    }
   ],
   "source": [
    "# 获取信息\n",
    "import requests\n",
    "import ssl \n",
    "keyword = animal_info1[\"result\"][0][\"name\"]\n",
    "try:\n",
    "    kv = {'wd':keyword}\n",
    "    r = requests.get('https://www.baidu.com/s',params=kv)\n",
    "    print(r.request.url)\n",
    "    r.raise_for_status()\n",
    "    r.encoding = r.apparent_encoding\n",
    "    print(len(r.text))\n",
    "except:\n",
    "    print(\"异常\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3248073",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
